package com.dd.ms.auth.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.ms.auth.mapper.UserMapper;
import com.dd.ms.auth.pojo.DO.UserDO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/7/8 17:42
 * @Description 用户操作仓储层
 */
@Repository
@RequiredArgsConstructor
public class UserRepository {

    private final UserMapper userMapper;

    public Boolean userExists(String account) {
        LambdaQueryWrapper<UserDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserDO::getAccount, account);
        UserDO userDO = userMapper.selectOne(lqw);
        return null != userDO;
    }

    public UserDO getUserByAccount(String account) {
        LambdaQueryWrapper<UserDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserDO::getAccount, account);
        return userMapper.selectOne(lqw);
    }

    public void save(UserDO userDO) {
        userMapper.insert(userDO);
    }

    public void updateById(UserDO userDO) {
        userMapper.updateById(userDO);
    }

    public String getUserApiKeyByAccount(String account) {
        LambdaQueryWrapper<UserDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserDO::getAccount, account);
        UserDO userDO = userMapper.selectOne(lqw);
        if(userDO.getAccessKey() == null || userDO.getAccessKey().isEmpty()) {
            return null;
        }
        return userDO.getAccessKey() + "#" + userDO.getSecretKey();
    }
}
